import Mock from 'mockjs'
import {lineDate} from '@/mock/line'
import users from '@/mock/users'
const Random = Mock.Random
const baseURL = 'http://localhost:8080'
import {nanoid} from 'nanoid'

let user = users
// 设置延时
Mock.setup({
    timeout: 400
})

Mock.mock(RegExp(`${baseURL}/getHomeData?` + '.*'),'get',(params) => {
    return {
        name: 'zs'
    }
})

Mock.mock(`${baseURL}/getHomeDatapost`,'post',(params) => {
    return {
        name: 'zs'
    }
})

// 折线图
Mock.mock(`${baseURL}/getLineData`,'post',(params) => {
    return [
        {
            name: '华为',
            type: 'line',
            
            data: lineDate().list.map((value,index) => {
                return value.id
            })
        },
        {
            name: '魅族',
            type: 'line',
            
            data: lineDate().list.map((value,index) => {
                return value.id
            })
        },
        {
            name: 'OPPO',
            type: 'line',
            
            data: lineDate().list.map((value,index) => {
                return value.id
            })
        },
        {
            name: '努比亚',
            type: 'line',
            
            data: lineDate().list.map((value,index) => {
                return value.id
            })
        },
        {
            name: '苹果',
            type: 'line',
            
            data: lineDate().list.map((value,index) => {
                return value.id
            })
        }
    ]
})

// 柱状图
Mock.mock(`${baseURL}/getBarData`,'post',(params) => {
    return [
        ['product', '新增用户', '活跃用户'],
        ['周一', Random.natural(0,20), Random.natural(50,80)],
        ['周二', Random.natural(0,20), Random.natural(50,80)],
        ['周三', Random.natural(0,20), Random.natural(50,80)],
        ['周四', Random.natural(0,20), Random.natural(50,80)],
        ['周五', Random.natural(0,20), Random.natural(50,80)],
        ['周六', Random.natural(0,20), Random.natural(50,80)],
        ['周日', Random.natural(0,20), Random.natural(50,80)]
    ]
})

// 饼图
Mock.mock(`${baseURL}/getPieData`,'post',(params) => {
    return [
        { value: Random.natural(800,1220), name: '华为' },
        { value: Random.natural(900,1220), name: '魅族' },
        { value: Random.natural(500,800), name: 'OPPO' },
        { value: Random.natural(500,920), name: '努比亚' },
        { value: Random.natural(700,1220), name: '苹果' }
    ]
})

// 查询用户（分页）
Mock.mock(`${baseURL}/getUsers`,'post',(params) => {
    var body = JSON.parse(params.body)
    console.log(body)
    return {
        users: user.slice((body.pageNum - 1) * body.pageSize,body.pageNum * body.pageSize),
        total: user.length
    }
})

// 创建用户
Mock.mock(`${baseURL}/addUsers`,'post',(params) => {
    var body = JSON.parse(params.body)
    body.id = nanoid()
    user.push(body)
    return user
})

// 更新用户
Mock.mock(`${baseURL}/updateUsers`,'post',(params) => {
    var body = JSON.parse(params.body)
    user = user.map(e => {
        if(e.id == body.id){
            return body
        }else{
            return e
        }
    })
    return user
})

// 删除用户
Mock.mock(`${baseURL}/delUsers`,'post',(params) => {
    var body = JSON.parse(params.body)
    user = user.filter(e => e.id != body.id)
    return user
})

import admin from '@/mock/admin'
import test from '@/mock/test'
// 用户登陆
Mock.mock(`${baseURL}/userLogin`,'post',(params) => {
    var body = JSON.parse(params.body)
    if(body.username == 'admin' && body.password == 'admin'){

        return {
            code:200,
            token:Random.guid(),
            permission: admin
        }
    }
    if(body.username == 'test' && body.password == '123456'){

        return {
            code:200,
            token:Random.guid(),
            permission: test
        }
    }
    return {
        code: 1
    }
})